/*
    Copyright 2009 Barnet Wagman

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package ot.fs;

import ot.*;
import java.io.*;

/** The {@link Context} for objects stored in the local files system.
 *  <tt>ContextFs</tt> is just a directory, specified by a <tt>File</tt>
 *  that represents the directory's full pathname.  In an <tt>ContextFs</tt>,
 *  an object's {@link OPath} is relative this directory.  E.g.
 *  Suppose the context directory is 
 *  <blockquote><tt>
 *      /home/user_name/ot_objects
 *  </tt></blockquote>
 *  We might have an object with <tt>OPath</tt>
 *  <blockquote><tt>
 *      new OPath(new String[] { "some","object.ser" })
 *  </tt></blockquote>
 *  Then the full pathname of the object in the underlying filesystem is
 *  <blockquote><tt>
 *      /home/user_name/ot_objects/some/object.ser
 *  </tt></blockquote>
 *
 * @author Barnet Wagman
 */
public class ContextFs implements Context, Serializable {

    static final long serialVersionUID = ConstantsOt.UID_VERSION;

    public File dir;

        /** If <tt>dir</tt> does not exists, the constructor will create
         *  it (and its parents if necessary).
         *
         * @param dir
         * @throws BadContextDirException if <tt>dir</tt> exists but is not a
         *  directory.
         */
    public ContextFs(File dir) throws OtException {

        if ( !dir.exists() ) dir.mkdirs();
        if ( !dir.isDirectory() )
            throw new OtException("A non-directory file named " +
                        dir.getPath() + " was specified as the context directory " +
                        " in ContextFs()");
        this.dir = dir;
    }

    public boolean equals(Object other) {
        if ( !(other instanceof ContextFs) ) return(false);
        else return( this.dir.equals( ((ContextFs) other).dir ) );
    }

    public String toString() {
        return(dir.getPath());
    }
}
